package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author LiangTianQi
 * @describe
 * @date 2020/11/9 22:01
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    //注入jedispool
    @Autowired
    private JedisPool jedisPool;

    @PostMapping("/getByConditions")
    public Result getByConditions(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = null;
        try {
            pageResult = setmealService.getByConditions(queryPageBean);
            return  new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 添加套餐和中间关系表
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.add(setmeal,checkgroupIds);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile) {
        try {
            //获取文件的原始名称
            String originalFilename = multipartFile.getOriginalFilename();
            int index = originalFilename.lastIndexOf(".");
            //获取文件的后缀名
            String suffix = originalFilename.substring(index - 1);
            //使用uuid生成随机的名称防止重复
            String fileName = UUID.randomUUID().toString() + suffix;
            QiniuUtils.upload2Qiniu(multipartFile.getBytes(),fileName);

            //向redis中保存上传成功的图片的名称
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);


            //返回数据
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    //getById通过id获取检查套餐的基本信息
    @GetMapping("/getById")
    public Result getById(Integer id) {
        try {
            Setmeal setmeal=setmealService.getById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    //getCheckGroupIds通过id获取中间表中checkgroup中所有的信息
    @GetMapping("/getCheckGroupIds")
    public Result getCheckGroupIds(Integer id) {
        try {
            List<Integer> checkGroupIds=setmealService.getCheckGroupIds(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,checkGroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    //update
    @PutMapping("/update")
    public Result update(@RequestBody Setmeal setmeal,Integer[] checkgroupIds) {
        try {
            setmealService.update(setmeal, checkgroupIds);
            return new Result(true,"修改套餐成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改套餐失败");
        }
    }

    //delete
    @GetMapping("/delete")
    public Result delete(Integer id) {
        try {
            setmealService.delete(id);
            return new Result(true,"删除检查套餐成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除检查套餐失败");
        }
    }



}
